package com.qk.management.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.qk.domain.blusiness.BusinessCountDO;
import com.qk.domain.clue.ClueCountDO;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.ClueMapper;
import com.qk.vo.portal.PortalVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RedisUpdateTimeUtil {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired//创建一个spring data redis对象操作
    private StringRedisTemplate springRedisTemlate;


    private static final String CACHE_PORTAL_KEY_PREFIX = "qk:portal:";

    // 每30分钟执行一次的定时任务
    @Scheduled(cron = "0 0/30 * * * ? ")
    public void updateCacheScheduled() {
        System.out.println("缓存更新定时任务开始执行...");
        String cacheKey = CACHE_PORTAL_KEY_PREFIX+"reportOverview";
        //获取线索
        ClueCountDO clueCountDO = clueMapper.getClueCountDO();
        //获取商机
        BusinessCountDO businessCountDO = businessMapper.getBusinessCountDO();
        //进行强转融合
        PortalVO portalVO = BeanUtil.copyProperties(clueCountDO, PortalVO.class);
        BeanUtil.copyProperties(businessCountDO, portalVO);

        try {
            String jsonStrToRedis = JSONUtil.toJsonStr(portalVO);
            springRedisTemlate.opsForValue().set(cacheKey, jsonStrToRedis);
        } catch (Exception e) {
            log.error("缓存出错{}"+e.getMessage());
        }
        System.out.println("缓存更新定时任务结束...");
    }
}
